package pl.ptemich.tankwars.physic;

public class PhysicTools {
  
  
  Derivative evaluate(State initial, float t, float dt, Derivative d) {
       State state = new State();
       state.position = initial.position + d.dx * dt;
       state.velocity  = initial.position + d.dv * dt;

       Derivative output = new Derivative();
       output.dx = state.velocity;
       output.dv = acceleration(state, t + dt);
       return output;
  }  

  float acceleration(State state, float t) {
       float k = 10;
       float b = 1;
       return -k * state.position - b * state.velocity;
  }
  
  void integrate(State state, float t, float dt) {
       Derivative a = evaluate(state, t, 0.0f, new Derivative());
       Derivative b = evaluate(state, t, dt*0.5f, a);
       Derivative c = evaluate(state, t, dt*0.5f, b);
       Derivative d = evaluate(state, t, dt, c);

       float dxdt = 1.0f/6.0f * (a.dx + 2.0f*(b.dx + c.dx) + d.dx);
       float dvdt = 1.0f/6.0f * (a.dv + 2.0f*(b.dv + c.dv) + d.dv);

       state.position = state.position + dxdt * dt;
       state.velocity = state.velocity + dvdt * dt;
  }  
  
}
